<template>
    <el-dialog
    title="上传证书照片"
    :visible="isShowPhotoUpload"
    @close="dialogClose">
        <el-form :model="ruleForm" :rules="rules" ref="uploadForm">
            <el-row :gutter="24">
                <el-col :span="24">
                    <el-form-item label="上传图片" prop="photo">
                        <el-upload
                        class="avatar-uploader"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        <div class="el-upload__tip" slot="tip">请按如下要求进行上传：格式：jpeg,jpg,git,bmp,png。</div>
                        </el-upload>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogClose">取 消</el-button>
            <el-button type="primary" @click="submitData()">确 定</el-button>
        </span>
        
    </el-dialog>
</template>

<script>
export default {
    data(){
          return{
              ruleForm: {
                  photo: '',
              },
              rules:{
                  photo:[
                      {required: true, message: '请选择'}
                  ],
              }
          }
      },
    props:{
        isShowPhotoUpload:{
            type:Boolean
        },
    },
    methods: {
        dialogClose(){ 
            this.$emit('dialogClose');
        },
        handleAvatarSuccess(res, file) {
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg/jpg/git/bmp/png';

            if (!isJPG) {
                this.$message.error('上传头像图片只能是jpeg/jpg/git/bmp/png格式!');
            }
            return isJPG;
        },
        submitData(){
            this.$refs.uploadForm.validate((valid) => {
                if (!valid) {
                    return
                }
                else {
                    this.$emit('dialogClose')
                }
            });
        },
    }

}
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>